package lingnan.cn.edu.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import lingnan.cn.edu.BaseAndImpl.MyService;
import lingnan.cn.edu.User.User;
import lingnan.cn.edu.util.DbAccess;

public class MyserviceImpl implements MyService{
	
	@Override
	public User login(String loginName,String password) {
		
		Connection conn = DbAccess.getConnection();
		User user = null;
		PreparedStatement prep = null;
		ResultSet rs=null;
		try {
			String SQL = "select * from staff where staffName = ? and password = ?";
		    prep = conn.prepareStatement(SQL);
			prep.setString(1,loginName);
		    prep.setString(2,password);
		    rs = prep.executeQuery();
		    while(rs.next()) {
		    user = new User();
			user.setLoginName(rs.getString("staffName"));
			user.setPassword(rs.getString("password"));
		}
		
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			DbAccess.release(conn, prep, rs);
		}
		
		return user;
		
	}

	@Override
	public List<User> query() {
		System.out.println("1");
		List<User> users = new ArrayList<User>();
		Connection conn = DbAccess.getConnection();
		String SQL = "select * from orderinf";
		
		PreparedStatement prep = null;
		ResultSet rs=null;
		try {
			
		    prep = conn.prepareStatement(SQL);
			rs = prep.executeQuery();
		    while(rs.next()) {
		    User user = new User();
		    user.setId(rs.getInt("orderid"));
			user.setLoginName(rs.getString("mName"));
			user.setPassword(rs.getString("mnum"));
			System.out.println(user.getId());
			System.out.println(user.getLoginName());
			System.out.println(user.getPassword());
			users.add(user);
		}
		
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			DbAccess.release(conn, prep, rs);
		}
		return users;
	}
	

}
